package com.hongxun.modules.api.controller;

import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.service.BDeviceQuantitiesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.api.entity.BDeviceEntity;
import com.hongxun.modules.api.service.BDeviceService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 配电设备信息表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-07-27 14:13:09
 */
@RestController
@RequestMapping("/api/bdevice")
public class BDeviceController {
	@Autowired
	private BDeviceService bDeviceService;
	@Autowired
	private BDeviceQuantitiesService bDeviceQuantitiesService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("dVoltage")!=null&&!query.get("dVoltage").toString().equals(""))
		{
			wheres+=" and  d_voltage = '" + query.get("dVoltage").toString()+"' ";
		}
		if(query.get("dName")!=null&&!query.get("dName").toString().equals(""))
		{
			wheres+=" and  d_name like '%" + query.get("dName").toString()+"%' ";
		}
		if(query.get("dLabel")!=null&&!query.get("dLabel").toString().equals(""))
		{
			wheres+=" and  d_label like '%" + query.get("dLabel").toString()+"%' ";
		}
		if(query.get("dIsmain")!=null&&!query.get("dIsmain").toString().equals(""))
		{
			wheres+=" and  d_ismain = '" + query.get("dIsmain").toString()+"' ";
		}
		if(query.get("dIshave")!=null&&!query.get("dIshave").toString().equals(""))
		{
			wheres+=" and ( d_ishave =0 or d_ishave  is null) ";
		}
		if(query.get("dBigtype")!=null&&!query.get("dBigtype").toString().equals(""))
		{
			wheres+=" and d_bigtype like '%" + query.get("dBigtype").toString()+"%' ";
		}
		if(query.get("dMidtype")!=null&&!query.get("dMidtype").toString().equals(""))
		{
			wheres+=" and d_midtype like '%" + query.get("dMidtype").toString()+"%' ";
		}

		query.put("filterSql",wheres+" ");
		List<BDeviceEntity> bDeviceList = bDeviceService.queryList(query);
		int total = bDeviceService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(bDeviceList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		BDeviceEntity bDevice = bDeviceService.queryObject(guid);
		
		return R.ok().put("bDevice", bDevice);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
public R save(@RequestBody BDeviceEntity bDevice){
		bDeviceService.save(bDevice);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
public R update(@RequestBody BDeviceEntity bDevice){
		bDeviceService.update(bDevice);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		bDeviceQuantitiesService.deleteByDguid(guids);
		bDeviceService.deleteBatch(guids);

		return R.ok();
	}

	@RequestMapping("/getBigtype")
	public R getBigtype(){
		List<BDeviceEntity> bigtypeList = bDeviceService.queryForBigtype();
		return R.ok().put("bigtype",bigtypeList);
	}

	@RequestMapping("/getMidtype")
	public R getMidtype(@RequestParam Map<String, Object> params){
		String bigtype = params.get("mBigtype").toString();
		List<BDeviceEntity> midtypeList = bDeviceService.queryForMidtype(bigtype);
		return R.ok().put("midtype",midtypeList);
	}
}
